/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rainful;

import java.util.concurrent.ConcurrentHashMap;
import rainful.echo.Echo;

/**
 *
 * @author Administrator
 */
public class TalkSubjectManager {

    protected static TalkSubjectManager instance;
    protected ConcurrentHashMap<String, TalkSubject> subjectMap;

    protected TalkSubjectManager() {
        subjectMap = new ConcurrentHashMap<String, TalkSubject>();
    }

    public static TalkSubjectManager getInstance() {
        if (instance == null) {
            instance = new TalkSubjectManager();
        }
        return instance;
    }

    public boolean register(String subjectName, TalkSubject subject) {
        if (!subjectMap.containsKey(subjectName)) {
            subjectMap.put(subjectName, subject);
            return true;
        } else {
            return false;
        }
    }

    public boolean exist(String subjectName) {
        return subjectMap.containsKey(subjectName);
    }

    public boolean exist(Echo echo){
        return this.exist(echo.getSubjectName());
    }

    public TalkSubject get(String subjectName) {
        return subjectMap.get(subjectName);
    }

    public TalkSubject get(Echo echo){
        return this.get(echo.getSubjectName());
    }
}
